#include<iostream>
#include <unordered_map>
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        unordered_map<char,int>need,windows;
        vector<int>ans;
        for(char c:p)need[c]++;
        int l=0,r=0,valid=0,len=p.size();
        while(r<s.size())
        {
            char c=s[r];
            r++;
            if(need.count(c))
            {
                windows[c]++;
                if(windows[c]==need[c])valid++;
            }
            while(r-l>=len)
            {
                if(valid==need.size())
                {
                    ans.push_back(l);
                }
                char a=s[l];
                l++;
                if(need.count(a))
                {
                    
                    if(windows[a]==need[a])valid--;
                    windows[a]--;
                }

            }
        }
        return ans;
    }
};